package com.banhujiu.spider.region

import com.banhujiu.spider.region.conf.queueSize
import com.banhujiu.spider.region.conf.siteUrl
import com.banhujiu.spider.region.enties.FileEntry
import com.banhujiu.spider.region.enties.JSEntry
import com.banhujiu.spider.region.enties.RegionEntry
import com.banhujiu.spider.region.enties.RegionVo
import com.banhujiu.spider.region.enums.AreaLevelEnum
import com.banhujiu.spider.region.factory.IOWrite
import com.banhujiu.spider.region.factory.JsFactory
import com.banhujiu.spider.region.factory.Province
import org.apache.logging.log4j.LogManager
import java.util.concurrent.Executors
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.TimeUnit

/**
 * @author banhujiu
 * @date 2017/11/19 0019 22:14
 */
class Bootstrap

class ProvinceQueue : LinkedBlockingQueue<RegionVo>(queueSize)
class JSQueue : LinkedBlockingQueue<JSEntry>(queueSize)
class IOQueue : LinkedBlockingQueue<RegionEntry>(queueSize)

private val logger = LogManager.getLogger(Bootstrap::class.java.name)

fun main(args: Array<String>) {
    logger.info("start")
    val file = FileEntry()
    val jsQueue = JSQueue()
    val provinceQueue = ProvinceQueue()
    val ioQueue = IOQueue()
    jsQueue.put(JSEntry(siteUrl, AreaLevelEnum.PROVINCE))
    val exec = Executors.newCachedThreadPool()
    exec.execute(JsFactory(jsQueue, provinceQueue))
    exec.execute(Province(provinceQueue, ioQueue, jsQueue))
    TimeUnit.SECONDS.sleep(10)
    logger.info("io start")
    exec.execute(IOWrite(ioQueue , file))
}
